package com.speechb.service.impl;

import com.speechb.bean.YearStatistics;
import com.speechb.common.DateUtils;
import com.speechb.mapper.dao.UserDao;
import com.speechb.service.IUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * <p>
 * 会员列表 服务实现类
 * </p>
 *
 * @author llj
 * @since 2020-04-10
 */
@Service
public class UserServiceImpl implements IUserService {
    private static final Logger LOGGER = LoggerFactory.getLogger(UserServiceImpl.class);

    @Autowired
    UserDao userMapper;

    @Override
    public int todayNewUser() {
        String todayDate = DateUtils.getCurrentFormatYYmmDD();
        String todayStart = todayDate + " 00:00:00";
        String todayEnd = todayDate + " 23:59:59";
        return userMapper.findUserCountByTime(todayStart, todayEnd);
    }

    @Override
    public List<YearStatistics> yearStatistics() {
        List<String> twelveMonths = DateUtils.getLastTwelveMonths();
        try {
            List<YearStatistics> list = new ArrayList<>(twelveMonths.size());
            YearStatistics yearStatistics = null;
            String monthEnd;
            String monthBegin;
            for (String months : twelveMonths) {
                yearStatistics = new YearStatistics();
                yearStatistics.setY(months);
                monthBegin = DateUtils.getMonthBegin(months);
                monthEnd = DateUtils.getMonthEnd(months);
                yearStatistics.setNew_user_count(userMapper.findUserCountByTime(monthBegin, monthEnd));
                list.add(yearStatistics);
            }
            return list;
        } catch (ParseException e) {
            LOGGER.warn("[yearStatistics]Incorrect date format.");
        }
        return Collections.emptyList();
    }

}
